home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / hf^k-6.dms / in.adf / Install.run / GOLDEDDATA / developer / examples / appicon / app.c next >
Encoding:
C/C++ Source or Header  |  1996-07-10  |  9.5 KB  |  461 lines

  1. /* -----------------------------------------------------------------------------
  2.  
  3.   GEDApp 1.1 - GoldED AppIcon handler, ©1996 Dietmar Eilert
  4.  
  5.   DICE:
  6.  
  7.   dcc app.c appIconA.a -// -proto -mRR -mi -pr -2.0 -o ram:AppIcon
  8.  
  9.  CONTENTS
  10.  
  11.   C source code of GEDApp.
  12.  
  13.  PURPOSE
  14.  
  15.    GEDApp is a simple AppIcon handler for the editor  GoldED.  It  will  put  an
  16.    Application  icon on the workbench screen. Icons of text files may be dragged
  17.    and dropped over this icon to make  GoldED  load  them  into  a  new  window.
  18.    Doubleclick at the AppIcon to make it disappear.
  19.  
  20.  INSTALLATION
  21.  
  22.    There is nothing to install - simply doubleclick GEDApp's icon to run it.  Or
  23.    copy  this  utility to your WBStartup drawer if you want to have it available
  24.    every time you boot your Amiga. GEDApp uses the  default  AppIcon  of  GoldED
  25.    itself, so GoldED should already be installed.
  26.  
  27.  HOW TO SET THE DEFAULT ICON POSITION ...
  28.  
  29.    Open the 'golded:icons' drawer. Move the  'AppIcon'  icon  from  within  that
  30.    directory to your preferred position, than snapshot it (workbench/icon menu).
  31.    Finally move the icon back to its drawer. GEDApp will read the  new  position
  32.    the next time it is evoked.
  33.  
  34.   ------------------------------------------------------------------------------
  35. */
  36.  
  37. /// "includes"
  38.  
  39. #include <amiga20/exec/exec.h>
  40. #include <string.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include <stdarg.h>
  45. #include <amiga20/intuition/intuition.h>
  46. #include <amiga20/dos/dos.h>
  47. #include <amiga20/dos/dosextens.h>
  48. #include <amiga20/dos/rdargs.h>
  49. #include <amiga20/dos/dostags.h>
  50. #include <amiga20/workbench/startup.h>
  51. #include <amiga20/workbench/workbench.h>
  52. #include <amiga20/rexx/errors.h>
  53. #include <amiga20/rexx/rxslib.h>
  54.  
  55. #include <amiga20/clib/alib_protos.h>
  56. #include <amiga20/clib/dos_protos.h>
  57. #include <amiga20/clib/exec_protos.h>
  58. #include <amiga20/clib/icon_protos.h>
  59. #include <amiga20/clib/intuition_protos.h>
  60. #include <amiga20/clib/utility_protos.h>
  61. #include <amiga20/clib/rexxsyslib_protos.h>
  62. #include <amiga20/clib/wb_protos.h>
  63.  
  64. #ifdef PRAGMAS
  65.  
  66. #include "Pragmas/exec.h"
  67. #include "Pragmas/disk.h"
  68. #include "Pragmas/diskfont.h"
  69. #include "Pragmas/dynamic.h"
  70. #include "Pragmas/gadtools.h"
  71. #include "Pragmas/keymap.h"
  72. #include "Pragmas/graphics.h"
  73. #include "Pragmas/icon.h"
  74. #include "Pragmas/input.h"
  75. #include "Pragmas/intuition.h"
  76. #include "Pragmas/layers.h"
  77. #include "Pragmas/locale.h"
  78. #include "Pragmas/misc.h"
  79. #include "Pragmas/timer.h"
  80. #include "Pragmas/wb.h"
  81. #include "Pragmas/xpkmaster.h"
  82. #include "Pragmas/amigaguide.h"
  83. #include "Pragmas/reqtools.h"
  84.  
  85. #endif
  86.  
  87. #define Prototype    extern
  88. #define MAX_LEN      150
  89.  
  90. ///
  91. /// "prototypes"
  92.  
  93. Prototype void   main(ULONG, char **);
  94. Prototype int    wbmain(struct WBStartup *);
  95. Prototype void   MainLoop(void);
  96. Prototype UBYTE *MakeFileName (UBYTE *, UBYTE *);
  97. Prototype UBYTE *CompletePath(UBYTE *);
  98. Prototype UBYTE *StartGED(void);
  99. Prototype struct RexxMsg *SendRexxCommand(UBYTE *, UBYTE *, struct MsgPort *);
  100. Prototype void   FreeRexxCommand (struct RexxMsg *);
  101. Prototype ULONG  WaitForAnswer(struct MsgPort *);
  102. Prototype UBYTE *LookForGED(void);
  103. Prototype void   ReadWBCmd(ULONG, struct WBArg *);
  104.  
  105. extern struct Library *IconBase;
  106. extern struct Library *DOSBase;
  107. extern struct Library *SysBase;
  108. extern struct Library *IntuitionBase;
  109. extern struct Library *WorkbenchBase;
  110.  
  111. // globals
  112.  
  113. UBYTE Version[] = "$VER: AppIcon 3.0 (" __COMMODORE_DATE__ ")";
  114.  
  115. ///
  116. /// "entry points"
  117.  
  118. void
  119. main(argc, argv)
  120.  
  121. ULONG argc;
  122. char *argv[];
  123. {
  124.     MainLoop();
  125. }
  126.  
  127. int
  128. wbmain(struct WBStartup *wbs)
  129. {
  130.     MainLoop();
  131. }
  132.  
  133.  
  134. ///
  135. /// "main loop"
  136.  
  137. /* --------------------------------- MainLoop ----------------------------------
  138.  
  139.  Open AppIcon, handle incoming messages
  140.  
  141. */
  142.  
  143. void
  144. MainLoop()
  145. {
  146.     struct DiskObject *appDiskObject;
  147.  
  148.     appDiskObject = GetDiskObject("golded:icons/appicon");
  149.  
  150.     if (appDiskObject == NULL)
  151.  
  152.         appDiskObject = GetDefDiskObject(WBTOOL);
  153.  
  154.     if (appDiskObject) {
  155.  
  156.         struct MsgPort    *msgPort;
  157.         struct AppMessage *amsg;
  158.         struct AppIcon    *appIcon;
  159.  
  160.         if (msgPort = CreateMsgPort()) {
  161.  
  162.             if (appIcon = AddAppIconA(0, NULL, "GoldED", msgPort, NULL, appDiskObject, TAG_END)) {
  163.  
  164.                 BOOL terminated = FALSE;
  165.  
  166.                 while (terminated == FALSE) {
  167.  
  168.                     while (amsg = (struct AppMessage *)GetMsg(msgPort)) {
  169.  
  170.                         if (amsg->am_NumArgs)
  171.                             ReadWBCmd(amsg->am_NumArgs, amsg->am_ArgList);
  172.                         else
  173.                             terminated = TRUE;
  174.  
  175.                         ReplyMsg((struct Message *)amsg);
  176.                     }
  177.  
  178.                     WaitPort(msgPort);
  179.                 }
  180.             }
  181.             else
  182.                 puts("Couldn't allocate AppIcon. Workbench closed ?!");
  183.  
  184.             RemoveAppIcon(appIcon);
  185.  
  186.             DeleteMsgPort(msgPort);
  187.         }
  188.         else
  189.             puts("Couldn't create message port ?!");
  190.  
  191.         FreeDiskObject(appDiskObject);
  192.     }
  193.     else
  194.         puts("Impossible d'allouer DiskObject ?!");
  195.  
  196.     exit(0);
  197. }
  198.  
  199.  
  200. ///
  201. /// "misc"
  202.  
  203. /* ------------------------------- MakeFileName --------------------------------
  204.  
  205.  Build fully qualified path from file/path names; return pointer to static copy.
  206.  
  207. */
  208.  
  209. UBYTE *
  210. MakeFileName(path, file)
  211.  
  212. UBYTE *path, *file;
  213. {
  214.     static UBYTE buffer[MAX_LEN + 1];
  215.  
  216.     strcpy(buffer, "\42");
  217.  
  218.     strcat(buffer, path);
  219.  
  220.     CompletePath(buffer);
  221.  
  222.     strcat(buffer, file);
  223.  
  224.     strcat(buffer, "\42");
  225.  
  226.     return(buffer);
  227. }
  228.  
  229. /* ------------------------------ CompletePath -----------------------------------
  230.  
  231.  Add '/' to path if missing so far
  232.  
  233. */
  234.  
  235. UBYTE *
  236. CompletePath(path)
  237.  
  238. UBYTE *path;
  239. {
  240.     UWORD len;
  241.  
  242.     if (len = strlen(path))
  243.  
  244.         if ((path[len - 1] != ':') && (path[len - 1] != '/'))
  245.  
  246.             strcat(path, "/");
  247.  
  248.     return(path);
  249. }
  250.  
  251. /* ---------------------------------- ReadWBCmd --------------------------------
  252.  
  253.  Parse AppIcon message
  254.  
  255. */
  256.  
  257. void
  258. ReadWBCmd(numArgs, argList)
  259.  
  260. ULONG  numArgs;
  261. struct WBArg  *argList;
  262. {
  263.     UBYTE *host;
  264.  
  265.     Forbid();
  266.  
  267.     host = LookForGED();
  268.  
  269.     Permit();
  270.  
  271.     if (host == NULL)
  272.  
  273.         host = StartGED();
  274.  
  275.     if (host) {
  276.  
  277.         struct MsgPort *replyPort;
  278.  
  279.         if (replyPort = CreateMsgPort()) {
  280.  
  281.             if (SendRexxCommand(host, "LOCK CURRENT RELEASE=4", replyPort)) {
  282.  
  283.                 if (WaitForAnswer(replyPort) == RC_OK) {
  284.  
  285.                     UBYTE path[MAX_LEN + 1];
  286.  
  287.                     UWORD  count;
  288.                     UBYTE *command;
  289.  
  290.                     for (count = 0; numArgs--; count++) {
  291.  
  292.                         NameFromLock(argList[count].wa_Lock, path, MAX_LEN);
  293.  
  294.                         command = MakeFileName(path, argList[count].wa_Name);
  295.  
  296.                         strins(command, "OPEN SMART QUIET ");
  297.  
  298.                         if (SendRexxCommand(host, command, replyPort))
  299.  
  300.                             WaitForAnswer(replyPort);
  301.                     }
  302.                 }
  303.  
  304.                 if (SendRexxCommand(host, "UNLOCK", replyPort))
  305.  
  306.                     WaitForAnswer(replyPort);
  307.             }
  308.  
  309.             DeleteMsgPort(replyPort);
  310.         }
  311.     }
  312. }
  313.  
  314.  
  315. /* ----------------------------------- LookForGED ----------------------------
  316.  
  317.  Look for running GoldED task
  318.  
  319. */
  320.  
  321. UBYTE *
  322. LookForGED()
  323. {
  324.     static UBYTE host[] = "GOLDED.1";
  325.  
  326.     UWORD try;
  327.  
  328.     for (try = '1'; try <= '9'; try++) {
  329.  
  330.         host[7] = try;
  331.  
  332.         if (FindPort(host))
  333.  
  334.             return(host);
  335.     } 
  336.  
  337.     return(NULL);
  338. }
  339.  
  340.  
  341. /* ------------------------------------- StartGED -----------------------------
  342.  
  343.  Launch a new GoldED task. Return pointer to host name (or NULL).
  344.  
  345. */
  346.  
  347. UBYTE *
  348. StartGED()
  349. {
  350.     struct MsgPort *port = NULL;
  351.  
  352.     if (SystemTags("GoldED:GoldED", SYS_Asynch, TRUE, SYS_Input, NULL, SYS_Output, NULL, TAG_DONE) == 0) {
  353.  
  354.         UWORD try;
  355.  
  356.         for (try = 50; try; (port == NULL) && try--, Delay(10)) {
  357.  
  358.             Forbid();
  359.  
  360.             port = FindPort("GOLDED.1");
  361.  
  362.             Permit();
  363.         }
  364.     }
  365.  
  366.     return((port) ? "GOLDED.1" : NULL);
  367. }
  368.  
  369. ///
  370. /// "ARexx"
  371.  
  372. /* -------------------------------------- WaitForAnswer -----------------------
  373.  
  374.   Wait for answer on previously sent message. Free message afterwards. Primary
  375.   return code is returned.
  376.  
  377. */
  378.  
  379. ULONG
  380. WaitForAnswer(port)
  381.  
  382. struct MsgPort *port;
  383. {
  384.     struct RexxMsg *rexxMsg;
  385.     ULONG  result;
  386.  
  387.     do {
  388.         
  389.         WaitPort(port);
  390.  
  391.         if (rexxMsg = (struct RexxMsg *)GetMsg(port))
  392.             result = rexxMsg->rm_Result1;
  393.  
  394.     } while (!rexxMsg);
  395.  
  396.     FreeRexxCommand(rexxMsg);
  397.  
  398.     return(result);
  399. }
  400.  
  401.  
  402. /* ------------------------------------- FreeRexxCommand ----------------------
  403.  
  404.  Free ARexx message
  405.  
  406. */
  407.  
  408. void
  409. FreeRexxCommand(rexxmessage)
  410.  
  411. struct RexxMsg *rexxmessage;
  412. {
  413.     if (rexxmessage->rm_Result1 == RC_OK) 
  414.  
  415.         if (rexxmessage->rm_Result2)
  416.  
  417.             DeleteArgstring((char *)rexxmessage->rm_Result2);
  418.  
  419.     DeleteArgstring((char *)ARG0(rexxmessage));
  420.  
  421.     DeleteRexxMsg(rexxmessage);
  422. }
  423.  
  424.  
  425. /* ---------------------------------- SendRexxCommand -------------------------
  426.  
  427.  Send ARexx message
  428.  
  429. */
  430.  
  431. struct RexxMsg *
  432. SendRexxCommand(port, cmd, replyPort)
  433.  
  434. struct MsgPort *replyPort;
  435. UBYTE          *cmd,   *port;
  436. {
  437.     struct MsgPort *rexxport;
  438.     struct RexxMsg *rexx_command_message = NULL;
  439.  
  440.     Forbid();
  441.  
  442.     if (rexxport = FindPort(port)) {
  443.  
  444.         if (rexx_command_message = CreateRexxMsg(replyPort, NULL, NULL)) {
  445.  
  446.             if (rexx_command_message->rm_Args[0] = CreateArgstring(cmd, strlen(cmd))) {
  447.  
  448.                 rexx_command_message->rm_Action = RXCOMM | RXFF_RESULT;
  449.  
  450.                 PutMsg(rexxport, &rexx_command_message->rm_Node);
  451.             }
  452.         }
  453.     }
  454.  
  455.     Permit();
  456.  
  457.     return(rexx_command_message);
  458. }
  459.  
  460. ///
  461.